package UserTable;
import user.User;
import utils.JdbcUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
public class UserTable extends Box {
    Font font1=new Font("宋体",Font.BOLD,25);
    Font font2=new Font("宋体",Font.BOLD,20);
    String sql_message="SELECT * FROM user";
    public UserTable(int power,String name){
        super(BoxLayout.Y_AXIS);
        Box user_sum_box=Box.createHorizontalBox();
        Box user_power_box= Box.createHorizontalBox();
        JLabel user_sum_title= new JLabel("现注册用户:"+User.sum+"人\n");
        JLabel user_power_title=new JLabel("欢迎"+name+"使用该系统"+",你的管理权限为:"+power);
        user_sum_title.setFont(font2);
        user_power_title.setFont(font1);
        user_sum_box.add(user_sum_title);
        user_power_box.add(user_power_title);
        this.add(user_sum_box);
        this.add(user_power_box);
        //创建按钮
        JPanel btnPanel =new JPanel();
        btnPanel.setMaximumSize(new Dimension(800,20));
        JButton addBtn=new JButton("添加");
        JButton updateBtn=new JButton("修改密码");
        JButton deleteBtn=new JButton("删除");
        JButton queryBtn=new JButton("查询");
        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(queryBtn);
        this.add(btnPanel);
        //添加按钮
        addBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Frame frame=new Frame();
                if(power>1)JOptionPane.showMessageDialog(frame,"您的权限不足");
                else AddUser.init(power,name);
            }
        });
        updateBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Frame frame=new Frame();
                if(power>1)JOptionPane.showMessageDialog(frame,"您的权限不足");
                else UpdataUser.init(power,name);
            }
        });
        deleteBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Frame frame=new Frame();
                if(power>1)JOptionPane.showMessageDialog(frame,"您的权限不足");
                else DeleteUser.init();
            }
        });
        queryBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                QueryUser.init();
            }
        });
        User.idChange();
        String[] ts={"用户编号","用户名","用户创建时间","用户最后登录时间","用户权限"};
        String[][] begin =data_update();
        JTable table =new JTable();
        table.setEnabled(false);
        DefaultTableModel model =new DefaultTableModel(begin,ts); // 新建一个默认数据模型
        JScrollPane scrollPane=new JScrollPane(table);
        table.setModel(model);
        this.add(scrollPane);
    }
    public String[][] data_update(){
        int MX= User.sum;
        String[][] data =new String[MX][5];
        int sum=0;
        List<Map<String, Object>> datas= JdbcUtils.JdbcQuery(sql_message);
        for(Map<String,Object> map : datas){
            int id = 0;
            String name = null;
            int power=0;
            LocalDateTime timebe=null;
            LocalDateTime timeup=null;
            // 在jdbc中时间戳类型通常被映射为Timestamp类型所以要多一步转化
            for(String key : map.keySet()) {
                if(key.equals("user_id")) id = (int) map.get(key);
                if(key.equals("user_name")) name = (String) map.get(key);
                if(key.equals("user_create_time")) {
                    Timestamp timestamp = (Timestamp) map.get(key);
                    timebe = timestamp != null ? timestamp.toLocalDateTime() : null;
                }
                if(key.equals("user_lastup_time")) {
                    Timestamp timestamp = (Timestamp) map.get(key);
                    timeup = timestamp != null ? timestamp.toLocalDateTime() : null;
                }
                if(key.equals("user_power")) power = (int) map.get(key);
            }
            data[sum][0] = String.valueOf(id);
            data[sum][1] = String.valueOf(name);
            data[sum][2] = timebe != null ? timebe.toString() : "";
            data[sum][3] = timeup != null ? timeup.toString() : "";
            data[sum][4] = String.valueOf(power);
            sum++;
        }
        return data;
    }
}
